require 'yaml'

class Global

  attr_reader :build_path, :cache_path

  def initialize(build_path, cache_path)
    @build_path = build_path
    @cache_path = cache_path
  end

  def build_pack_dir
    @build_pack_dir ||= File.dirname(File.dirname(__FILE__))
  end

  def url_yml_path
      @url_yml_path ||= File.join(build_pack_dir, 'config', 'url.yml')
  end

  def resources_php_dir
    @resources_php_dir ||= File.join(build_pack_dir, 'resources', 'php')
  end

  def resources_nginx_dir
    @resources_nginx_dir ||= File.join(build_pack_dir, 'resources', 'nginx')
  end

  def dependency_yml_path
    @dependency_yml_path ||= File.join(build_path, 'config', 'dependency.yml')
  end

  def dependency_yml
    if File.exist? dependency_yml_path
      @dependency_yml ||= YAML.load_file(dependency_yml_path)
    else
      return nil
    end
  end

  def url_yml
    @url_yml ||= YAML.load_file(url_yml_path)
  end

  def remote_nginx_url
    base = url_yml['base']
    if dependency_yml and not dependency_yml['nginx'].nil?
      File.join(base, dependency_yml['nginx'])
    else
      File.join(base, url_yml['default']['nginx'])
    end
  end

  def remote_php_url
    base = url_yml['base']
    if dependency_yml and not dependency_yml['php'].nil?
      File.join(base, dependency_yml['php'])
    else
      File.join(base, url_yml['default']['php'])
    end
  end

  def tmp_nginx_path
    File.join(cache_path, 'nginx.tar.gz')
  end

  def tmp_php_path
    File.join(cache_path, 'php.tar.gz')
  end

  def target_nginx_tarball
    File.join(nginx_dir, 'nginx.tar.gz')
  end

  def target_php_tarball
    File.join(php_dir, 'php.tar.gz')
  end

  def nginx_dir
    File.join(build_path, 'nginx')
  end

  def php_dir
    File.join(build_path, 'php')
  end

  def user_nginx_conf
    File.join(build_path, 'config', 'nginx.conf')
  end

  def user_mime_types
    File.join(build_path, 'config', 'mime.types')
  end

  def user_php_ini
    File.join(build_path, 'config', 'php.ini')
  end

  def user_php_fpm_conf
    File.join(build_path, 'config', 'php-fpm.conf')
  end

  def target_nginx_conf
    File.join(nginx_dir, 'conf', 'nginx.conf')
  end

  def target_mime_types
    File.join(nginx_dir, 'conf', 'mime.types')
  end

  def target_php_ini
    File.join(php_dir, 'etc', 'php.ini')
  end

  def target_php_fpm_conf
    File.join(php_dir, 'etc', 'php-fpm.conf')
  end

  def php_tar_out_dir
    File.join(cache_path, 'php.tar.out')
  end

  def nginx_tar_out_dir
    File.join(cache_path, 'nginx.tar.out')
  end

  def default_php_ini(version)
    File.join(resources_php_dir, version, 'php.ini')
  end

  def default_php_fpm_conf(version)
    File.join(resources_php_dir, version, 'php-fpm.conf')
  end

  def default_nginx_conf
    File.join(resources_nginx_dir, 'nginx.conf')
  end

  def default_mime_types
    File.join(resources_nginx_dir, 'mime.types')
  end
end